package zozopo.warehouse.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zozopo.comm.ZOZOPOCODE;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class BackreceiptlistOS extends OperatingService{
	
	
	public BackreceiptlistOS(Connection conn){
		super(conn);
	}
	
	public BackreceiptlistOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<BackreceiptlistTB> selectListBySql(String sql){
		List<BackreceiptlistTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		//Flow flow = new Flow(conn);
		//Map flowMap = flow.getFlowStateMapByKind(FlowCode.HOUSEWARE_RECEIPTLIST);
		if(resultSet != null){
				try {
					list = new ArrayList<BackreceiptlistTB>();
					while(resultSet.next()){
						BackreceiptlistTB obj = new BackreceiptlistTB();
						
						obj.setId_backreceiptlist(resultSet.getInt(BackreceiptlistTB.ID_BACKRECEIPTLIST));
						obj.setDepartment_backreceiptlist(resultSet.getString(BackreceiptlistTB.DEPARTENT_BACKRECEIPTLIST));
						obj.setDepartmentid_backreceiptlist(resultSet.getInt(BackreceiptlistTB.DEPARTENTID_BACKRECEIPTLIST));
						
						obj.setGoodsnumber_backreceiptlist(resultSet.getInt(BackreceiptlistTB.GOODSNUMBER_BACKRECEIPTLIST));
						obj.setReceiptlistid_backreceiptlist(resultSet.getInt(BackreceiptlistTB.RECEIPTLISTID_BACKRECEIPTLIST));
						obj.setRemark_backreceiptlist(resultSet.getString(BackreceiptlistTB.REMARK_BACKRECEIPTLIST));
						obj.setShipprice_backreceiptlist(resultSet.getDouble(BackreceiptlistTB.SHIPPRICE_BACKRECEIPTLIST));
						
						
						obj.setState_backreceiptlist(resultSet.getString(BackreceiptlistTB.STATE_BACKRECEIPTLIST));
						obj.setTotal_backreceiptlist(resultSet.getDouble(BackreceiptlistTB.TOTAL_BACKRECEIPTLIST));
						
						obj.setUseremail_backreceiptlist(resultSet.getString(BackreceiptlistTB.USEREMAIL_BACKRECEIPTLIST));
						obj.setUsername_backreceiptlist(resultSet.getString(BackreceiptlistTB.USERNAME_BACKRECEIPTLIST));
						obj.setDate_backreceiptlist(resultSet.getString(BackreceiptlistTB.DATE_BACKRECEIPTLIST));
						
						
						list.add(obj);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(BackreceiptlistTB backreceiptlist){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		insertMap.put(BackreceiptlistTB.DEPARTENT_BACKRECEIPTLIST, backreceiptlist.getDepartment_backreceiptlist());
		
		insertMap.put(BackreceiptlistTB.DEPARTENTID_BACKRECEIPTLIST, backreceiptlist.getDepartmentid_backreceiptlist());
		
		
		insertMap.put(BackreceiptlistTB.GOODSNUMBER_BACKRECEIPTLIST, backreceiptlist.getGoodsnumber_backreceiptlist());
		
		insertMap.put(BackreceiptlistTB.RECEIPTLISTID_BACKRECEIPTLIST, backreceiptlist.getReceiptlistid_backreceiptlist());
		
		insertMap.put(BackreceiptlistTB.REMARK_BACKRECEIPTLIST, backreceiptlist.getRemark_backreceiptlist());
		insertMap.put(BackreceiptlistTB.SHIPPRICE_BACKRECEIPTLIST, backreceiptlist.getShipprice_backreceiptlist());
		insertMap.put(BackreceiptlistTB.TOTAL_BACKRECEIPTLIST, backreceiptlist.getTotal_backreceiptlist());
		
		insertMap.put(BackreceiptlistTB.USEREMAIL_BACKRECEIPTLIST, backreceiptlist.getUseremail_backreceiptlist());
		
		insertMap.put(BackreceiptlistTB.USERNAME_BACKRECEIPTLIST, backreceiptlist.getUsername_backreceiptlist());
		
	
		
		return sqlMachine.insertRow(BackreceiptlistTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxIdByEmail(String email){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(BackreceiptlistTB.ID_BACKRECEIPTLIST, BackreceiptlistTB.TABLE_NAME, BackreceiptlistTB.USEREMAIL_BACKRECEIPTLIST, email);
		return maxId;
	}
	
	public BackreceiptlistTB get(int id){
		String sql = "SELECT * FROM " + BackreceiptlistTB.TABLE_NAME + " where " + BackreceiptlistTB.ID_BACKRECEIPTLIST + "=" + id;
		BackreceiptlistTB obj = new BackreceiptlistTB();
		List<BackreceiptlistTB> list = selectListBySql(sql);
		obj = list.get(0); 
		return obj;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public BackreceiptlistTB getBackreceiptlistByReceiptlistid(int id){
		String sql = "SELECT * FROM " + BackreceiptlistTB.TABLE_NAME + " where " + BackreceiptlistTB.RECEIPTLISTID_BACKRECEIPTLIST + "=" + id;
		BackreceiptlistTB obj = new BackreceiptlistTB();
		List<BackreceiptlistTB> list = selectListBySql(sql);
		if(list.size() == 0){
			return null;
		}
		obj = list.get(0); 
		return obj;
	}
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<BackreceiptlistTB> selectAll(){
		String sql = "SELECT * FROM " + BackreceiptlistTB.TABLE_NAME;
		List<BackreceiptlistTB> list =  selectListBySql(sql);
		
		return list;
	}
	
	public List<BackreceiptlistTB> selectAll(int beginPageNum,int pageSize){
		
		List<BackreceiptlistTB> list = null;
		String sql = "SELECT * FROM " + BackreceiptlistTB.TABLE_NAME  
		+ " ORDER BY " + BackreceiptlistTB.ID_BACKRECEIPTLIST + " DESC "
		+ " limit "+beginPageNum+","+pageSize; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	public List<BackreceiptlistTB> selectByReceiptlistid(int receiptlistid){
		
		List<BackreceiptlistTB> list = null;
		String sql = "SELECT * FROM " + BackreceiptlistTB.TABLE_NAME  
		+ " where " + BackreceiptlistTB.RECEIPTLISTID_BACKRECEIPTLIST + "=" + receiptlistid
		
		+ " ORDER BY " + BackreceiptlistTB.ID_BACKRECEIPTLIST + " DESC ";
		
		list = selectListBySql(sql);
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + BackreceiptlistTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<BackreceiptlistTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		String sql = "select * from "+BackreceiptlistTB.TABLE_NAME+" WHERE  "
		+BackreceiptlistTB.ID_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.DATE_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.DEPARTENT_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.DEPARTENTID_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		
		+" OR "+BackreceiptlistTB.RECEIPTLISTID_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.REMARK_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.USEREMAIL_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.USERNAME_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+ " ORDER BY " + BackreceiptlistTB.DATE_BACKRECEIPTLIST + " DESC "
		
		+" limit "+beginPageNum+","+pageSize; 
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		List<BackreceiptlistTB> receiptlistList = selectListBySql(sql);
		
		return receiptlistList;
	}
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + BackreceiptlistTB.TABLE_NAME +" WHERE  "
		+BackreceiptlistTB.ID_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.DATE_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.DEPARTENT_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.DEPARTENTID_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		
		+" OR "+BackreceiptlistTB.RECEIPTLISTID_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.REMARK_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.USEREMAIL_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'"
		+" OR "+BackreceiptlistTB.USERNAME_BACKRECEIPTLIST+" LIKE '%"+searchStr+"%'";
		
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean cancle(int id){
		
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(BackreceiptlistTB.STATE_BACKRECEIPTLIST, ZOZOPOCODE.CANCELED);
		
		//sqlMachine.updateRow(SupplierTB.TABLE_NAME, updateMap, PurchaseTB.ID_PURCHASE, id);
		
		return sqlMachine.updateRow(BackreceiptlistTB.TABLE_NAME, updateMap, BackreceiptlistTB.ID_BACKRECEIPTLIST, id);
		
		
	}
	
	
	

}
